package test1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class GUI extends JFrame {
    private final JTextArea resultArea;
    private final JFileChooser fileChooser;

    public GUI() {
        setTitle("经典软件体系结构教学软件 陈岩 202231061306");
        setSize(700, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        JButton processButton = new JButton("点击此处选择目标文件处理KWIC索引");
        processButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                processKWIC();
            }
        });
        panel.add(processButton);

        resultArea = new JTextArea();
        resultArea.setEditable(false);
        add(panel, BorderLayout.NORTH);
        add(new JScrollPane(resultArea), BorderLayout.CENTER);

        fileChooser = new JFileChooser();

        setVisible(true); // 使框架可见

    }

    private void processKWIC() {
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            String content = FileUtility.readFile(file);
            String kwicResult = KWICProcessor.process(content);
            resultArea.setText(kwicResult);
        }
    }
}
